﻿Imports Phila
Imports Phila.Sql
Imports Phila.CommonClasses

Public Class EditMealType
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            'If Request.Cookies("Admin") Is Nothing Then
            If Session.Item("UserID") Is Nothing Then
                'Response.Redirect("Login.aspx")
            End If
            If Not IsPostBack = True Then
                If Not Request.QueryString("id") Is Nothing Then
                    fillData(Request.QueryString("id"))
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub fillData(ByVal Id As Integer)
        Dim arr As Array
        arr = RetriveData("select Id, Name, Summary, IsActive from ptn_MealType where id=" & Id & " order by Name", 3)
        txtName.Text = Server.HtmlDecode(arr(1))
        txtSummary.Text = Server.HtmlDecode(arr(2))
        chkActive.Checked = arr(3)
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Dim strActive As String
            If chkActive.Checked = True Then
                strActive = 1
            Else
                strActive = 0
            End If
            If Request.QueryString("Id") Is Nothing Then
                Dim arr As Array
                arr = RetriveData("Select Id from ptn_MealType where Name='" & Trim(txtName.Text.Replace("'", "&#039;")) & "'", 0)
                If arr(0) Is Nothing Then
                    Insert_Update_Delete_Record("INSERT INTO [ptn_MealType]([Name],[Summary],[IsActive]) VALUES('" & Server.HtmlEncode(txtName.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtSummary.Text.Replace("'", "&#039;")) & "'," & strActive & ")")
                    Response.Redirect("ViewMealType.aspx")
                Else
                    lblMsg.Text = "Meal Type already exist."
                End If
            Else
                Insert_Update_Delete_Record("Update [ptn_MealType] set [Name]='" & Server.HtmlEncode(txtName.Text.Replace("'", "&#039;")) & "',[Summary]='" & Server.HtmlEncode(txtSummary.Text.Replace("'", "&#039;")) & "',[IsActive]=" & strActive & " where Id=" & Request.QueryString("Id"))
                Response.Redirect("ViewMealType.aspx")
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect("ViewMealType.aspx")
    End Sub
End Class